    //
//  MenuViewController.m
//  11111
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MenuViewController.h"
#import "MenuTableViewCell.h"
#import "TotalViewController.h"
#import "DetailViewController.h"


@interface MenuViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *picArray; // 图片数组

@property (nonatomic, assign)NSInteger index;

@property (nonatomic, strong) NSArray *urlArray;

@property (nonatomic, strong) NSMutableArray *dataArray;// 热门美食

@property (nonatomic, strong)NSMutableArray *liangtangArray; // 靓汤

@property (nonatomic, strong)NSMutableArray *shishangtianpingArray; // 时尚甜品
@property (nonatomic, strong)NSMutableArray *liangcaiArray; // 凉菜
@property (nonatomic, strong)NSMutableArray *teshexiaochiArray; // 特设小吃
@property (nonatomic, strong)NSMutableArray *tianpingArray; // 甜品
@property (nonatomic, strong)NSMutableArray *yaoshanmeishiArray; // 药膳美食
@property (nonatomic, strong)NSMutableArray *hongpeiArray; // 烘培

@end

@implementation MenuViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
      self.allUrlArray = [NSMutableArray array];
    [self setUpData];
    
    self.index = 0;
    

// 热门美食
 [[DefaultManager defaultManager]  requestReMenMeiShiDataForReloadWithUrl:reMenMeiShiUrl Block:^(NSArray *array) {
     self.dataArray = (NSMutableArray *)array;
     [self.tableView reloadData];
     
 } ];
    // 靓汤
 [[DefaultManager defaultManager]  requestLiangTangDataForReloadWithUrl:laingTangUrl Block:^(NSArray *array) {
     self.liangtangArray = (NSMutableArray *)array;
     [self.tableView reloadData];
    } ];

//    // 特色小吃
 [[DefaultManager defaultManager]  requestTeSeXiaoChiDataForReloadWithUrl:teSeXiaoChiUrl Block:^(NSArray *array) {
     self.teshexiaochiArray = (NSMutableArray *)array;
     [self.tableView reloadData];
    } ];
//
//    // 凉菜
 [[DefaultManager defaultManager]  requestLiangCaiDataForReloadWithUrl:laingCaiUrl Block:^(NSArray *array) {
     self.liangcaiArray = (NSMutableArray *)array;
     [self.tableView reloadData];
    } ];
//
//    // 时尚饮品
 [[DefaultManager defaultManager]  requestShiShangYinPinDataForReloadWithUrl:shiShangYinPinUrl Block:^(NSArray *array) {
     self.shishangtianpingArray = (NSMutableArray *)array;
     [self.tableView reloadData];
    } ];
//
//    // 甜品
 [[DefaultManager defaultManager]  requestTianPinnDataForReloadWithUrl:tianPinUrl Block:^(NSArray *array) {
     self.tianpingArray = (NSMutableArray *)array;
     [self.tableView reloadData];
    } ];

    // 药膳美食
  [[DefaultManager defaultManager]  requestYaoShanShiLiaoDataForReloadWithUrl:yaoShanMeiShiUrl Block:^(NSArray *array) {
      self.yaoshanmeishiArray = (NSMutableArray *)array;
      [self.tableView reloadData];
    //  [self addTableView];

    } ];

    // 烘培
  [[DefaultManager defaultManager]  requestHongPeiDataForReloadWithUrl:hongPeiUrl Block:^(NSArray *array) {
      self.hongpeiArray = (NSMutableArray *)array;
      [self.tableView reloadData];
    //  [self addTableView];

  } ];

   [self addTableView];
   
}


- (void)addTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    self.scrollView.delegate = self;
   
}

- (void)setUpData
{
    self.picArray = [NSArray arrayWithObjects:@"ReMenMeiShi.jpg", @"LiangTang.jpg", @"TeSeXiaoChi.jpg", @"LiangCai.jpg", @"ShiShangYinPin.jpg", @"TianPinn.jpg", @"YaoShanShiLiao.jpg", @"HongPei.jpg", nil];
    self.fenleiArray = [NSArray arrayWithObjects:@"热门美食", @"靓汤", @"特色小吃", @"凉菜", @"时尚饮品", @"甜品", @"药膳食疗", @"烘培", nil];
}

// cell高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 360 * kWidth;
}

// 分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.picArray.count;
}

// 分区数
- ( NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
 static   NSString *identifier = @"MyCell";
    MenuTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //MenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MenuTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.leftImage.image = [UIImage imageNamed:@"left"];
    cell.rightLabel.textAlignment = NSTextAlignmentLeft;
    cell.rightLabel.font = [UIFont systemFontOfSize:20];
    cell.rightLabel.text = self.fenleiArray[indexPath.row];
    // 大图片按钮
    cell.bigButton.imageView.image = [UIImage imageNamed:self.picArray[indexPath.row]];
    [cell.bigButton setImage:[UIImage imageNamed:self.picArray[indexPath.row]] forState:(UIControlStateNormal)];
    [cell.bigButton addTarget:self action:@selector(actionBigButton:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.bigButton.backgroundColor = [UIColor clearColor];
   
    // cell 赋值图片
    if (indexPath.row == 0) {
      //  self.model0 = [[MenuModel alloc] init];
        self.model0 = self.dataArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:self.model0.thumb] ];
     //   self.model1 = [[MenuModel alloc] init];
        self.model1 = self.dataArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:self.model1.thumb]];
      //  self.model2 = [[MenuModel alloc] init];
        self.model2 = self.dataArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:self.model2.thumb]];
      //  self.model3 = [[MenuModel alloc] init];
        self.model3 = self.dataArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:self.model3.thumb]];


    }else if (indexPath.row == 1) {
        self.model = [[MenuModel alloc] init];
        self.model = self.liangtangArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:self.model.thumb] ];
        self.model1 = [[MenuModel alloc] init];
        self.model1 = self.liangtangArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:self.model1.thumb]];
        self.model2 = [[MenuModel alloc] init];
        self.model2 = self.liangtangArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:self.model2.thumb]];
        self.model3 = [[MenuModel alloc] init];
        self.model3 = self.liangtangArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:self.model3.thumb]];
    } else if (indexPath.row == 2) {
        MenuModel *model = [[MenuModel alloc] init];
        model = self.teshexiaochiArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:model.thumb] ];
        MenuModel *model1 = [[MenuModel alloc] init];
        model1 = self.teshexiaochiArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:model1.thumb]];
        MenuModel *model2 = [[MenuModel alloc] init];
        model2 = self.teshexiaochiArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:model2.thumb]];
        MenuModel *model3 = [[MenuModel alloc] init];
        model3 = self.teshexiaochiArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:model3.thumb]];
    } else if (indexPath.row == 3) {
        MenuModel *model = [[MenuModel alloc] init];
        model = self.liangcaiArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:model.thumb] ];
        MenuModel *model1 = [[MenuModel alloc] init];
        model1 = self.liangcaiArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:model1.thumb]];
        
        MenuModel *model2 = [[MenuModel alloc] init];
        model2 = self.liangcaiArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:model2.thumb] ];
        MenuModel *model3 = [[MenuModel alloc] init];
        model3 = self.liangcaiArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:model3.thumb]];
    } else if (indexPath.row == 4) {
        MenuModel *model0 = [[MenuModel alloc] init];
        model0 = self.shishangtianpingArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:model0.thumb] ];
        MenuModel *model1 = [[MenuModel alloc] init];
        model1 = self.shishangtianpingArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:model1.thumb]];
        
        MenuModel *model2 = [[MenuModel alloc] init];
        model2 = self.shishangtianpingArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:model2.thumb] ];
        MenuModel *model3 = [[MenuModel alloc] init];
        model3 = self.shishangtianpingArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:model3.thumb]];
    } else if (indexPath.row == 5) {
        MenuModel *model = [[MenuModel alloc] init];
        model = self.tianpingArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:model.thumb] ];
        MenuModel *model1 = [[MenuModel alloc] init];
        model1 = self.tianpingArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:model1.thumb]];
        
        MenuModel *model2 = [[MenuModel alloc] init];
        model2 = self.tianpingArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:model2.thumb] ];
        MenuModel *model3 = [[MenuModel alloc] init];
        model3 = self.tianpingArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:model3.thumb]];
        
        
    } else if (indexPath.row == 6) {
        
        MenuModel *model = [[MenuModel alloc] init];
        model = self.yaoshanmeishiArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:model.thumb] ];
        MenuModel *model1 = [[MenuModel alloc] init];
        model1 = self.yaoshanmeishiArray[4];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:model1.thumb]];
        
        MenuModel *model2 = [[MenuModel alloc] init];
        model2 = self.yaoshanmeishiArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:model2.thumb] ];
        MenuModel *model3 = [[MenuModel alloc] init];
        model3 = self.yaoshanmeishiArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:model3.thumb]];
        NSLog(@"----------%@", model3.title);
        
    } else if (indexPath.row == 7) {
        
        MenuModel *model = [[MenuModel alloc] init];
        model = self.hongpeiArray[0];
        [cell.image11 sd_setImageWithURL:[NSURL URLWithString:model.thumb] ];
        MenuModel *model1 = [[MenuModel alloc] init];
        model1 = self.hongpeiArray[1];
        [cell.image21 sd_setImageWithURL:[NSURL URLWithString:model1.thumb]];
        
        MenuModel *model2 = [[MenuModel alloc] init];
        model2 = self.hongpeiArray[2];
        [cell.image31 sd_setImageWithURL:[NSURL URLWithString:model2.thumb] ];
        MenuModel *model3 = [[MenuModel alloc] init];
        model3 = self.hongpeiArray[3];
        [cell.image41 sd_setImageWithURL:[NSURL URLWithString:model3.thumb]];
        
    }
    
    [cell.button1 addTarget:self action:@selector(actionButton1:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.button2 addTarget:self action:@selector(actionButton2:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.button3 addTarget:self action:@selector(actionButton3:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.button4 addTarget:self action:@selector(actionButton4:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.button5 setImage:[UIImage imageNamed:@"SeeAll.jpg"] forState:(UIControlStateNormal)];
    [cell.button5 addTarget:self action:@selector(actionButton5:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}

- (void)actionBigButton:(UIButton *)button
{
    
    UIView *v = [button superview];
    UIView *v1 = [v superview];
    MenuTableViewCell *cell = (MenuTableViewCell *)[v1 superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"---index.row == %ld", index.row);
    NSArray *array = [NSArray array];
   array = [[DefaultManager defaultManager] addAllArrayUrl] ;
    
    TotalViewController *totalVC = [[TotalViewController alloc] init];
   // NSArray *array = self.allUrlArray[index.row];
    totalVC.AllArrayUrl1 = array;
    totalVC.index = index.row;
    [self.navigationController pushViewController:totalVC animated:YES];
}

- (void)actionButton1:(UIButton *)button
{
     DetailViewController *detailView = [[DetailViewController alloc] init];
    // scrollview
    UIView *v = [button superview];
    // backgroudview
    UIView *v1 = [v superview];
    UIView *v2 = [v1 superview];
    MenuTableViewCell *cell = (MenuTableViewCell *)[v2 superview];
    // 取出self.tableView的父视图
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"index.row = %ld", index.row);
    if (index.row == 0) {
        self.model = self.dataArray[0];
        detailView.model = self.model;
    }else if (index.row == 1) {
        self.model0 = self.liangtangArray[0];
        detailView.model = self.model0;
    }else if (index.row == 2) {
        self.model1 = self.teshexiaochiArray[0];
        detailView.model = self.model1;
    }else if (index.row == 3) {
        self.model2 = self.liangcaiArray[0];
        detailView.model = self.model2;
    }else if (index.row == 4) {
        self.model3 = self.shishangtianpingArray[0];
        detailView.model = self.model3;
    }else if (index.row == 5) {
        self.model4 = self.tianpingArray[0];
        detailView.model = self.model4;
    }else if (index.row == 6) {
        self.model5 = self.yaoshanmeishiArray[0];
        detailView.model = self.model5;
    }else if (index.row == 7) {
        self.model6 = self.hongpeiArray[0];
        detailView.model = self.model6;
    }

    [self.navigationController pushViewController:detailView animated:YES];
}

- (void)actionButton2:(UIButton *)button
{
    DetailViewController *detailView = [[DetailViewController alloc] init];
    // scrollview
    UIView *v = [button superview];
    // backgroudview
    UIView *v1 = [v superview];
    UIView *v2 = [v1 superview];
    MenuTableViewCell *cell = (MenuTableViewCell *)[v2 superview];
    // 取出self.tableView的父视图
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"index.row = %ld", index.row);
    if (index.row == 0) {
        self.model = self.dataArray[1];
        detailView.model = self.model;
    }else if (index.row == 1) {
        self.model0 = self.liangtangArray[1];
        detailView.model = self.model0;
    }else if (index.row == 2) {
        self.model1 = self.teshexiaochiArray[1];
        detailView.model = self.model1;
    }else if (index.row == 3) {
        self.model2 = self.liangcaiArray[1];
        detailView.model = self.model2;
    }else if (index.row == 4) {
        self.model3 = self.shishangtianpingArray[1];
        detailView.model = self.model3;
    }else if (index.row == 5) {
        self.model4 = self.tianpingArray[1];
        detailView.model = self.model4;
    }else if (index.row == 6) {
        self.model5 = self.yaoshanmeishiArray[1];
        detailView.model = self.model5;
    }else if (index.row == 7) {
        self.model6 = self.hongpeiArray[1];
        detailView.model = self.model6;
    }
    
    [self.navigationController pushViewController:detailView animated:YES];

}

- (void)actionButton3:(UIButton *)button
{
    DetailViewController *detailView = [[DetailViewController alloc] init];
    // scrollview
    UIView *v = [button superview];
    // backgroudview
    UIView *v1 = [v superview];
    UIView *v2 = [v1 superview];
    MenuTableViewCell *cell = (MenuTableViewCell *)[v2 superview];
    // 取出self.tableView的父视图
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"index.row = %ld", index.row);
    if (index.row == 0) {
        self.model = self.dataArray[2];
        detailView.model = self.model;
    }else if (index.row == 1) {
        self.model0 = self.liangtangArray[2];
        detailView.model = self.model0;
    }else if (index.row == 2) {
        self.model1 = self.teshexiaochiArray[2];
        detailView.model = self.model1;
    }else if (index.row == 3) {
        self.model2 = self.liangcaiArray[2];
        detailView.model = self.model2;
    }else if (index.row == 4) {
        self.model3 = self.shishangtianpingArray[2];
        detailView.model = self.model3;
    }else if (index.row == 5) {
        self.model4 = self.tianpingArray[2];
        detailView.model = self.model4;
    }else if (index.row == 6) {
        self.model5 = self.yaoshanmeishiArray[2];
        detailView.model = self.model5;
    }else if (index.row == 7) {
        self.model6 = self.hongpeiArray[2];
        detailView.model = self.model6;
    }
    
    [self.navigationController pushViewController:detailView animated:YES];

}

- (void)actionButton4:(UIButton *)button
{
    DetailViewController *detailView = [[DetailViewController alloc] init];
    // scrollview
    UIView *v = [button superview];
    // backgroudview
    UIView *v1 = [v superview];
    UIView *v2 = [v1 superview];
    MenuTableViewCell *cell = (MenuTableViewCell *)[v2 superview];
    // 取出self.tableView的父视图
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"index.row = %ld", index.row);
    if (index.row == 0) {
        self.model = self.dataArray[3];
        detailView.model = self.model;
    }else if (index.row == 1) {
        self.model0 = self.liangtangArray[3];
        detailView.model = self.model0;
    }else if (index.row == 2) {
        self.model1 = self.teshexiaochiArray[3];
        detailView.model = self.model1;
    }else if (index.row == 3) {
        self.model2 = self.liangcaiArray[3];
        detailView.model = self.model2;
    }else if (index.row == 4) {
        self.model3 = self.shishangtianpingArray[3];
        detailView.model = self.model3;
    }else if (index.row == 5) {
        self.model4 = self.tianpingArray[3];
        detailView.model = self.model4;
    }else if (index.row == 6) {
        self.model5 = self.yaoshanmeishiArray[3];
        detailView.model = self.model5;
    }else if (index.row == 7) {
        self.model6 = self.hongpeiArray[3];
        detailView.model = self.model6;
    }
    
    [self.navigationController pushViewController:detailView animated:YES];
    
}

- (void)actionButton5:(UIButton *)button
{    
    UIView *v = [button superview];
    UIView *v1 = [v superview];
    MenuTableViewCell *cell = (MenuTableViewCell *)[v1 superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"---index.row == %ld", index.row);
    NSArray *array = [NSArray array];
    array = [[DefaultManager defaultManager] addAllArrayUrl] ;
    
    TotalViewController *totalVC = [[TotalViewController alloc] init];
    // NSArray *array = self.allUrlArray[index.row];
    totalVC.AllArrayUrl1 = array;
    totalVC.index = index.row;
    [self.navigationController pushViewController:totalVC animated:YES];
    
}


@end
